<?php
/********************************************************************
 *	Author:
 *		Jestery Liu <jestery@gmail.com>
 *		http://www.pte.cn
 *
 *	File:			CliResponse.class.php
 *	Create Date:	2007-05-18 17:26:36 CST
 *******************************************************************/

require_once(PTE::getCoreLibsPath().'Response.interface.php');
require_once(PTE::getCoreLibsPath().'Event.class.php');

class CliResponse implements IResponse
{
	private $body = array();
	
	public function __construct($outputBuffing = true, $obHandler = 'ob_gzhandler')
	{
	}

	public function setHttpResponseCode($code)
	{
		throw new Exception('HTTP_RESPONSE_CODE does not supported under cli');
	}
	public function getHttpResponseCode()
	{
		throw new Exception('HTTP_RESPONSE_CODE does not supported under cli');
	}
	public function addHeader($headerName, $value, $replace=false)
	{
		throw new Exception('HEADER does not supported under cli');
	}
	public function addRawHeader($header, $replace=false)
	{
		throw new Exception('HEADER does not supported under cli');
	}
	public function addNoCacheHeaders()
	{
		throw new Exception('HEADER does not supported under cli');
	}
	public function clearHeaders()
	{
		throw new Exception('HEADER does not supported under cli');
	}
	public function getHeaders()
	{
		throw new Exception('HEADER does not supported under cli');
	}
	public function canSendHeaders()
	{
		throw new Exception('HEADER does not supported under cli');
	}
	public function sendHeaders()
	{
		throw new Exception('HEADER does not supported under cli');
	}
	public function add404Header()
	{
		throw new Exception('HEADER does not supported under cli');
	}

	public function setRedirect($url)
	{
		throw new Exception('HEADER does not supported under cli');
	}

	public function setCookie($var, $value, $life = 0)
	{
		throw new Exception('COOKIE does not supported under cli');
	}
	
	public function addBodyContent($content, $name='default')
	{
		Event::notifyEvent($this, 'preAddBodyContent', array('content'=>&$content, 'name'=>&$name));
		$this->body[$name] = (string)$content;
		Event::notifyEvent($this, 'postAddBodyContent', array('content'=>&$content, 'name'=>&$name));
	}
	public function appendBodyContentToSegment($content, $name='default')
	{
		Event::notifyEvent($this, 'preAppendBodyContentToSegment', array('content'=>&$content, 'name'=>&$name));
		$this->body[$name].=(string)$content;
		Event::notifyEvent($this, 'postAppendBodyContentToSegment', array('content'=>&$content, 'name'=>&$name));
	}
	public function prependBodyContentToSegment($content, $name='default')
	{
		Event::notifyEvent($this, 'prePrependBodyContentToSegment', array('content'=>&$content, 'name'=>&$name));
		$this->body[$name] = (string)$content.$this->body[$name];
		Event::notifyEvent($this, 'postPrependBodyContentToSegment', array('content'=>&$content, 'name'=>&$name));
	}
	public function insertBodyContent($content, $name, $segName=null, $before=false)
	{
		Event::notifyEvent($this, 'preInsertBodyContent',
		array('content'=>&$content, 'name'=>&$name, 'segName'=>&$segName, 'before'=>&$before));
		if ($segName===null || !isset($this->body[$segName]))
		{
			$this->addBodyContent($content, $name);
		}
		$keys = array_keys($this->body);
		$pos = array_search($segName, $keys)+1;
		if ($before)
		{
			$pos--;
		}
		array_splice($this->body, $pos, 0, array('name'=>$content));
		Event::notifyEvent($this, 'postInsertBodyContent',
		array('content'=>&$content, 'name'=>&$name, 'segName'=>&$segName, 'before'=>&$before));
	}
	public function sendBody()
	{
		Event::notifyEvent($this, 'preSendBody', array('body'=>&$this->body));
		foreach ($this->body as $content)
		{
			echo $content;
		}
		Event::notifyEvent($this, 'postSendBody', array('body'=>&$this->body));
		$this->clearBody();
	}
	public function clearBody($name=null)
	{
		Event::notifyEvent($this, 'preClearBody', array('name'=>&$name));
		if ($name===null)
		{
			$this->body = array();
		}
		else
		{
			unset($this->body[$name]);
		}
		Event::notifyEvent($this, 'postClearBody', array('name'=>&$name));
	}
	public function getBody()
	{
		return $this->body;
	}
	public function send($flush=true)
	{
		Event::notifyEvent($this, 'preSend', array('flush'=>&$flush, 'body'=>&$this->body));
		$this->sendBody();
		Event::notifyEvent($this, 'postSend', array('flush'=>&$flush));
	}
	public function __toString()
	{
		foreach ($this->body as $content) {
			$return.=$content;
		}
		return $return;
	}
}

/* vim: set ts=4 sw=4 ff=unix: */
?>